﻿using ConfExpert.Forms.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using DevTeam;
using ConfExpert.Datasets;
using DevExpress.XtraGrid.Columns;
using ConfExpert.Utility;
using DevExpress.XtraGrid.Views.Grid;

namespace ConfExpert.Forms.Depo.Kauf
{
    public partial class SatirCogalt : XForm
    {
        MyData.OrdersRow row;
        private int FirstRowsHandle;
        public SatirCogalt(MyData.OrdersRow row)
        {
            this.row = row;
            InitializeComponent();
        }
        MyData.OrdersDataTable dt = null;
        private void SatirCogalt_Load(object sender, EventArgs e)
        {
            this.Text = "Beden Dağılımı";
            MyData.DagilimRow r = AppSession.Lookups.Dagilim.FindByDagilimId(row.DagilimId);

            txtInfo.Caption = row.idx + " | " + "Çoğaltılacak sipariş satırı için gerekli miktarları girin.";

            dt = new MyData.OrdersDataTable();
            dt.ImportRow(row);
            dt.Rows.Add(dt.NewRow());

            gv.Init(dt);

            List<string> colNames = new List<string>();
            for (int i = 0; i < dt.Columns.Count + 1; i++)
                colNames.Add(gv.Columns[i].FieldName);

            colNames.Add("ProductId");
            gv.MakeColumnInvisible(colNames.ToArray());

            if (r != null)
            {
                for (int i = 1; i < 29; i++)
                {
                    string fieldName = String.Format("A{0:00}", i);
                    string cap = r[String.Format("A{0:00}", i)].ToString();
                    if (cap != "")
                    {
                        gv.Columns[fieldName].Caption = cap != string.Empty ? cap : " ";
                        gv.Columns[fieldName].VisibleIndex = i;
                    }
                }
            }

            gv.OptionsView.ShowAutoFilterRow = false;
            gv.Appearance.FocusedCell.BackColor = Color.YellowGreen;
            //gv.EnableNewRow();
            gv.ShowingEditor += gv_ShowingEditor;

            gv.BestFitColumns();
            gv.Focus();
            FirstRowsHandle = gv.FocusedRowHandle;
            btnSave.ItemClick += btnSave_ItemClick;
        }

        void gv_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView view = sender as GridView;

            if (view.FocusedRowHandle==FirstRowsHandle)
                e.Cancel = true;
        }

        void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.EndEditing();
            string strUpdate = "update Orders set ";
            int FormedStringLength = strUpdate.Length;
            bool SetIfadesiEklendi = false;

            for (int i = 0; i < dt.Rows.Count; ++i)
            {
                foreach (GridColumn col in gv.VisibleColumns)
                {
                    if (i == 0)
                        break;

                    int val1 = dt.Rows[i][col.FieldName].ToInt();
                    
                    if (val1 > 0)
                    {
                        strUpdate += col.FieldName + "=" + val1 + ",";
                        SetIfadesiEklendi = true;
                    }
                }

                if (i != (dt.Rows.Count - 1))
                {
                    if (strUpdate.Length > FormedStringLength)
                    {
                        strUpdate = strUpdate.RemoveIfEndsWith(",");
                        strUpdate += " where ";
                        strUpdate += ";update Orders set ";
                    }
                }
                else
                    break;

                FormedStringLength = strUpdate.Length;
                SetIfadesiEklendi = false;
            }

            if (SetIfadesiEklendi)
            {
                strUpdate = strUpdate.RemoveIfEndsWith(",");
                strUpdate += " where ";
            }
            else
            {
                strUpdate = strUpdate.Substring(0, strUpdate.Length - 19);
            }

            InsertString = strUpdate;

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void gv_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.RowHandle == 0)
            {
                gv.FocusedRowHandle = 1;
            }
        }

        public string InsertString = "";
    }
}
